package de.uni_mannheim.informatik.ai.dame_ai.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CsvAccess {
	private static final String SEPERATOR = ";";
	private static final Pattern PAIR_PAT= Pattern.compile("(.+)"+SEPERATOR+"(.+)");
	private static final Pattern FILENAME=Pattern.compile("(.+)\\.csv");
	
	/**
	 * Returns a (little complex^^) data Structure that contains all available<br/>
	 * language data from the file system.
	 * @param game The game for which the language data is needed.
	 * @return A Map which contains Maps. </br>
	 * 					Every language "x" is represented as one Map, which maps English
	 * 					phrases, which are used inside the game, to their translation in "x".
	 * 					The Maps which contain the data of certain Languages are wrapped into
	 * 					an outer map, which maps them to their names.
	 * 					examples:
	 * 					1)getLangs(FileSystem.GameName.mill).getValue("Deutsch")
	 * 					: Map<String, String>
	 * 					
	 * 					2)getLangs(FileSystem.GameName.mill).getValue("Deutsch").getValue("Beer's cool!")
	 * 					: "Bier ist kühl!"
	 */
	public static Map<String, Map<String, String>> getLangs(FileSystem.GameName game){
		File[] langFiles = FileSystem.getLanguageFiles(game);
		HashMap<String, Map<String, String>> langs = new HashMap<String, Map<String, String>>();
		for(File langFile: langFiles){
			String fileName = langFile.getName();
			Matcher fNameMat = FILENAME.matcher(fileName);
			if(fNameMat.find()){
				String langName=fNameMat.group(1);
				HashMap<String, String> lang = new HashMap<String, String>();
				String content = FileAccess.readFile(langFile);
				Matcher mat = PAIR_PAT.matcher(content);
				while(mat.find())
					lang.put(mat.group(1).trim(), mat.group(2).trim());
				if(!lang.isEmpty())
					langs.put(langName, lang);
			}
		}
		return langs;
	}

	
	public static void main(String[] args) {
		Map<String, Map<String, String>> langs = getLangs(FileSystem.GameName.mill);
		Map<String, String> deutsch = langs.get("Deutsch");
		System.out.println(deutsch.keySet());
		System.out.println(deutsch.get("wellcome"));
		System.out.println(deutsch.get("mayPut"));
	}
}